<%@ page import="com.cj.peiwan.server.order.IOrderService" %>
<%@ page import="com.cj.peiwan.server.order.OrderServiceFactory" %>
<%@ page import="com.cj.peiwan.server.other.Global" %>
<%@ page import="org.dom4j.Document" %>
<%@ page import="org.dom4j.DocumentHelper" %>
<%@ page import="org.dom4j.Element" %>
<%@ page import="org.wei.utils.IOUtil" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.SortedMap" %>
<%@ page import="java.util.TreeMap" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    try {
        InputStream is = request.getInputStream();
        String s = IOUtil.inStream2String(is, "utf-8");
        Document document = DocumentHelper.parseText(s);
        Element root = document.getRootElement();
        String out_trade_no = root.element("out_trade_no").getText().split("-")[0];
        SortedMap<String, Object> map = new TreeMap<>();
        List<Element> list = root.elements();
        for (Element node : list) {
            if (!node.getName().equals("sign")) {
                map.put(node.getName(), node.getText());
            }
        }
        String sign = Global.wxSign(map);
        if (sign.equals(root.element("sign").getText())) {
            //验证签名成功
            IOrderService orderService = OrderServiceFactory.create();
            orderService.payRecharge("bihumanbu123A1!@abc",
                    Long.valueOf(out_trade_no));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("xml");
    root.addElement("return_code")
            .addText("SUCCESS");

%>
